$(function () {
    // 初始化
    doPrice();

    // 鼠标以上全选框变色效果
    $('.fa-check').mouseover(function () {
        if ($(this).attr('class') != 'fa fa-check checked') {
            $(this).css('color', '#00ffea');
        }
    });

    $('.fa-check').mouseleave(function () {
        $(this).css('color', '#fbfbfb');
    });

    // 普通选中
    $('.fa-check').click(function () {
        let cla = $(this).attr('class');
        if (cla != 'fa fa-check checked') {
            $(this).attr('class', 'fa fa-check checked');
        } else {
            $(this).attr('class', 'fa fa-check');
        }
        doCheckAll();
        doPrice();
    });

    // 全部选中
    $('#check-all').click(function () {
        let cla = $(this).attr('class');
        if (cla !== 'fa fa-check checked') {
            $('.fa-check').attr('class', 'fa fa-check checked');
        } else {
            $('.fa-check').attr('class', 'fa fa-check');
        }
        doPrice();
    });

    // 检查是否全选
    function doCheckAll() {
        let allitem = $('.list-item i[class*="fa-check"]').length;
        let checkeditem = $('.list-item i[class$="checked"]').length;
        if (allitem !== checkeditem) {
            $('#check-all').attr('class', 'fa fa-check');
        } else {
            $('#check-all').attr('class', 'fa fa-check checked');
        }
    }


    // 加+按钮
    $('.plus').click(function () {
        var nowvalue = $(this).siblings('input').val();
        var nowvalue = parseInt(nowvalue);
        let currentvalue = nowvalue + 1;
        $(this).siblings('input').val(currentvalue);
        let price = parseFloat($(this).parents('.good-num').siblings('.good-price').html());
        let total = price * currentvalue;
        $(this).parents('.good-num').siblings('.good-total-price').html(total + '元');
        doPrice()
    });

    // 减-按钮
    $('.minus').click(function () {
        var nowvalue = $(this).siblings('input').val();
        var nowvalue = parseInt(nowvalue);
        let currentvalue = 0;
        nowvalue <= 1 ? currentvalue = 1 : currentvalue = nowvalue - 1;
        $(this).siblings('input').val(currentvalue);
        let price = parseFloat($(this).parents('.good-num').siblings('.good-price').html());
        let total = price * currentvalue;
        $(this).parents('.good-num').siblings('.good-total-price').html(total + '元');
        doPrice()
    });


    // 统计所有勾选框的值
    function doPrice() {
        //选中小计价格
        let checkeditems = $('.list-item i[class$="checked"]').parents('.select').siblings('.good-total-price');
        let totalprice = 0; // 存储总价
        for (let i = 0; i < checkeditems.length; i++) {
            totalprice += parseFloat(checkeditems[i].innerHTML);
        }

        $('.sum-price').html(totalprice);


        // 选中多少件

        $('.select-count').html(checkeditems.length);


        // 总商品数量

        let items = $('.list-item i[class*="fa-check"]');

        $('.all-count').html(items.length);

    }


    // 删除商品

    $('.operation').click(function () {

        $(this).parents('.list-item').remove();

    });

});

